<?php

///////ADD(POST)///////
//name - nickname
//text - message

//Set refresh
header("Expires: Mon, 26 Jul 2003 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, no-store,  must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//Variables
$max_mes_in_DB=30;

// init DB
  $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
  mysql_select_db('rcms_chat', $link) or die ('Can\'t use "rcms_chat" : ' . mysql_error());
  
// Convert html & php to text
function html2text($text='')
{
	$text = stripslashes($text);
	$text = htmlspecialchars($text);
	$text = str_replace("\r\n", "<br>", $text);
	return $text;
}

// add message
  function add()
  {
  	global $link;
  	$query="INSERT into chat ('nickname', 'site', 'text', 'data', 'ip') VALUES ('".$name."', '".$_SERVER['HTTP_HOST']."', '".$text."', '".date("Y-m-d G:i:s")."', '".getenv('REMOTE_ADDR')."')";
  	$name='anonimus';
  	$text='';
  	if (isset($_POST['name'])) $name=html2text($_POST['name']);
  	if (isset($_POST['text'])) $name=html2text($_POST['text']);
  	mysql_query($query) or die("Could not add message: " . mysql_error());
  }  
  
// get messages
  function show()
  {
  	global $link;
  	$query='SELECT nickname, site, text, data from chat';
  	$result = mysql_query($query) or die("Could not show messages: " . mysql_error());
  	while ($mes = mysql_fetch_array($result, MYSQL_ASSOC))
  	{
  		echo '';
  	}
  }  
  
// main logic
if (isset($_POST['action']))
  {
  	if ($_POST['action'] == 'add') add();
  }
  show();
  echo(date("Y-m-d G:i:s"));
  
    
// close connection to DB 
  mysql_close($link);

?>